<?php
/**
 * House Controller for Congregation Component
 * 
 * @package    Congregation
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * House Controller
 *
 * @package    Congregation
 * @subpackage Components
 */
class CongregationControllerHouse extends CongregationController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	 protected $_p_id;
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
	
		$this->registerTask( 'add'  , 	'edit' );
		$this->registerTask( 'new'  , 	'edit' );
		$this->registerTask( 'orderup',         'order');
		$this->registerTask( 'orderdown',       'order');
		$this->_p_id = JRequest::getCmd('p_id');
	}

	
	function display()
	{
		JRequest::setVar( 'view', 'houses' );
		parent::display();
	}
	
	
	
	/**
	 * display the edit form
	 * @return void
	 */
	function edit()
	{
		JRequest::setVar( 'view', 'house' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$model = $this->getModel('house');

		if ($model->store()) {
			$msg = JText::_( 'House Saved!' );
		} else {
			$msg = JText::_( 'Error Saving House' );
		}
		
		// Check the table in so it can be edited.... we are done with it anyway
		$link = 'index.php?option=com_congregation&controller=house&p_id=' . $this->_p_id;
		$this->setRedirect($link, $msg);
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel('house');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or More House Could not be Deleted' );
		} else {
			$msg = JText::_( 'House Deleted' );
		}

		$this->setRedirect( 'index.php?option=com_congregation&controller=house&p_id=' . $this->_p_id, $msg );
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=house&p_id=' . $this->_p_id, $msg );
	}
	
  /**
   * Moves the order of a province
   *
   * @access  public
   * @return  void
   * @since   1.2.0
   */
  function order()
  {
    $cid = JRequest::getVar('cid', array(), 'post', 'array');

    // Direction
    $dir  = 1;
    $task = JRequest::getCmd('task');
    if($task == 'orderup')
    {
      $dir = -1;
    }
	
    if(isset($cid[0]))
    {
      $row = & JTable::getInstance('house', 'Table');
	  $row->load((int)$cid[0]);
      $row->move($dir, " p_id='" . $this->_p_id . "' ");
      $row->reorder(" p_id='" . $this->_p_id . "' ");
    }
	$this->setRedirect('index.php?option=com_congregation&controller=house&p_id=' . $this->_p_id);

  }

	/**
	* Saves the order of the provinces
	*
	* @access  public
	* @return  void
	* @since   1.2.0
	*/
	function saveOrder()
	{
	
	
	$cid    = JRequest::getVar('cid', array(0), 'post', 'array');
	$order  = JRequest::getVar('order', array (0), 'post', 'array');

	// Create and load the categories table object
	$row = & JTable::getInstance('house', 'Table');

	// Update the ordering for items in the cid array
	for($i = 0; $i < count($cid); $i ++)
	{
	  $row->load((int)$cid[$i]);
	  if($row->ordering != $order[$i])
	  {
		$row->ordering = $order[$i];
		if(!$row->store())
		{
		  JError::raiseError( 500, $this->_db->getErrorMsg() );
		  return false;
		}
	  }
	}

	$row->reorder(" p_id='" . $this->_p_id . "' ");

	$msg = JText::_('Ordering saved');
	$this->setRedirect('index.php?option=com_congregation&controller=house&p_id=' . $this->_p_id, $msg);
	}
	
}